package com;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLUpdate {
	public XMLUpdate(){
		super();
	}
	
	public void write(Document doc){
    	try {
    		XMLWriter writer;      
            OutputFormat format = OutputFormat.createPrettyPrint();      
            format.setEncoding("utf-8");
            FileOutputStream fos = new FileOutputStream(FileURL.getXMLURL());           
            writer = new XMLWriter(fos, format);      
            writer.write(doc);      
            writer.close();      
        } catch (IOException e) {    
            e.printStackTrace();      
        } 
    }
	
	public void insertAppNode(Document doc, Element appNode, int num,
			String name, String url){
		Element el = appNode.addElement("webapp").addAttribute("id", num+"");
		el.addElement("name").setText(name);
		el.addElement("url").setText(url);
		
		write(doc);
	}

	public void insertUserNode(Document doc, 
			String userId, String userName,String regType,
			String university, String appName, String appURL){
		Element root = doc.getRootElement();
		Element author = root.addElement("author").addAttribute("id", userId);
		author.addElement("userName").setText(userName);
		author.addElement("regType").setText(regType);
		author.addElement("university").setText(university);
		
		Element apps = author.addElement("webapps");
		
		insertAppNode(doc,apps,0,appName,appURL);
	}
	
	public  String insertXML(String userId, String userName,String regType,
			String university, String appName, String appURL){
		String retStr = "";
		
		try{
			SAXReader reader = new SAXReader();
			Document doc = reader.read(FileURL.getXMLURL());
			
			Element root = doc.getRootElement();
			Iterator it = root.elements("author").iterator();
			
			while(it.hasNext()){
				Element el = (Element)it.next();
				if(el.attributeValue("id").equals(userId)){
					Element appNode = el.element("webapps");
					Iterator apps = appNode.elements("webapp").iterator();
					int counter = 0;
					while(apps.hasNext()){
						apps.next();
						counter ++;
					}
					
					if(counter < 5){
						insertAppNode(doc,appNode,counter,
								appName, appURL);
						retStr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;谢谢您的使用，您还有" + (5-counter-1)  + "个附加应用可以添加！<br/>"
							+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回首页可添加更多应用";
					}else{
						retStr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;谢谢您的使用，但是您已经提交了5个应用，达到了我们所能支持的上限，谢谢您的支持！<br>";						
					}
					
					return retStr;
				}
			}
			
			insertUserNode(doc, userId,userName,regType,university,appName,appURL);
			LoadAppString.setUserList(userId, userName);
			retStr = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;谢谢您的使用，您还有4个附加应用可以添加！<br>"
				+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回首页可添加更多应用";
		}catch(DocumentException de){
			de.printStackTrace();
		}
		
		return retStr;
	}
}
